﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;

namespace MII_FIF.Layouts.MII_FIF.Master.Location
{
    public partial class ViewLocation : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                btnAdd.Click += btnAdd_Click;
                btnDelete.Click += btnDelete_Click;
                GridLocation.RowCommand += GridLocation_RowCommand;
                GridLocation.PageIndexChanging += GridLocation_PageIndexChanging;
                //((Label)Header.FindControl("lblHeader")).Text = "View Location";
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void GridLocation_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridLocation.DataSource = (DataTable)ViewState["Location"];
            GridLocation.PageIndex = e.NewPageIndex;
            GridLocation.DataBind();
        }

        void GridLocation_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Trim() == "edit")
            {
                Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Location/AddLocation.aspx?ID=" + e.CommandArgument));
            }
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool delete = false;
            DataTable Location = (DataTable)ViewState["Location"];
            InventoryManagement.Location IM = new InventoryManagement.Location();
            int stat = 0;
            foreach (GridViewRow row in GridLocation.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("chk");
                if (chk != null)
                    if (chk.Checked)
                    {
                        delete = true;
                        break;
                    }
            }

            if (delete)
            {
                for (int i = GridLocation.Rows.Count - 1; i > -1; i--)
                {
                    CheckBox chk = (CheckBox)GridLocation.Rows[i].FindControl("chk");
                    if (chk != null)
                        if (chk.Checked)
                        {
                            Location.Rows.RemoveAt(i);
                            IM.ID = Guid.Parse(GridLocation.Rows[i].Cells[3].Text.ToString());
                            IM.Kode = GridLocation.Rows[i].Cells[1].Text.ToString();
                            IM.Nama = GridLocation.Rows[i].Cells[2].Text.ToString();
                            IM.IsDeleted = true;
                            stat = IM.Update();
                            if (stat < 0)
                            {
                                lblError.Text += "Delete Failed"; break;
                            }
                        }
                }
                GridLocation.DataSource = Location;
                GridLocation.DataBind();
                ViewState["Location"] = Location;
            }
            else
            {
                lblError.Text += "Please check at least 1 item to delete <br />";
            }
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Location/AddLocation.aspx"));
        }

        void load()
        {
            InventoryManagement.Location location = new InventoryManagement.Location();
            location.IsDeleted = false;
            ViewState["Location"] = location.SelectByParam();
            GridLocation.DataSource = (DataTable)ViewState["Location"];
            GridLocation.DataBind();
        }
    }
}
